//
//  ChosenViewController.m
//  BuffetNews
//
//  Created by 李荃荃 on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ChosenViewController.h"
#import "ModelSelect.h"

#import "UIColor+myColor.h"
#import "MBProgressHUD.h"
#import "myTableViewCell.h"
#import "UIImage+Tint.h"
#import "WeiPlist.h"
#import "myViewCell.h"
#import "SecondViewController.h"
#import "SecondaryPages.h"
#import "UIImageView+WebCache.h"
#import "NightModel.h"
#import "WebColor.h"

@interface ChosenViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) ModelSelect * model;
@property (nonatomic, retain) UITableView * table;
@property (nonatomic, retain) UIImageView * iamge;
@property (nonatomic, retain) UIImageView * iamge1;
@property (nonatomic, retain) UIImageView * iamge2;
@property (nonatomic, retain) UIImageView * iamge3;
@property (nonatomic, retain) MBProgressHUD * loading;
@property (nonatomic, retain) ModelSelect * model1;
@property (nonatomic, retain) NSMutableArray * arr1B;
@property (nonatomic, retain) NSMutableArray * arr1c;
@property (nonatomic, retain) UIImageView * iamge4;
@property (nonatomic, retain) UIImageView * iamge5;
@property (nonatomic, retain) UIImageView * iamge6;
@property (nonatomic, retain) UIImageView * iamge7;
@property (nonatomic, retain) UIImageView * iamge8;
@end

@implementation ChosenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


// 取数据
- (void)startHoms
{
    WeiPlist * plist = [[WeiPlist alloc] init];
 
    NSDictionary *rootArray = [plist dictionaryData:@"CachedFeaturesData"];
    // 精选title
    self.model = [[ModelSelect alloc] initWithDictionary:rootArray];

    NSDictionary *rootArray1 = [plist dictionaryData:@"CachedFeaturesPromotion"];
    self.model1 = [[ModelSelect alloc] initWithDictionary:rootArray1];
    [self.table reloadData];

}
- (void)viewWillAppear:(BOOL)animated
{

    [self.table reloadData];
}
// 加载试图
- (void)startTableView
{
    self.navigationController.navigationBar.translucent = NO;
    
    NSArray *tempArr = [NSArray array];
    NSArray *tempArr1 = [NSArray array];
    NSMutableArray *listImage = [NSMutableArray array];
    NSMutableArray *listName = [NSMutableArray array];
    NSMutableArray *listImage1 = [NSMutableArray array];
    NSMutableArray *listName1 = [NSMutableArray array];
    tempArr = self.model1.list[0];
    tempArr1 = self.model1.list[1];
    for (NSDictionary *tempdic in tempArr) {
        [listImage addObject:[tempdic objectForKey:@"promotion_img"]];
        [listName addObject:[tempdic objectForKey:@"title"]];
    }
    for (NSDictionary *tempdic1 in tempArr1) {
        [listImage1 addObject:[tempdic1 objectForKey:@"promotion_img"]];
        [listName1 addObject:[tempdic1 objectForKey:@"title"]];
    }
    

    self.iamge1 = [[UIImageView alloc] initWithFrame:CGRectMake(5 + self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width - 10 , self.view.frame.size.height / 7)];
    
    [self.iamge1 sd_setImageWithURL:[NSURL URLWithString:listImage[0]]];
    
    self.iamge2 = [[UIImageView alloc] initWithFrame:CGRectMake(5 + self.view.frame.origin.x, self.view.frame.size.height / 7 + 5, self.view.frame.size.width / 2 - 5, self.view.frame.size.height / 7- 5)];
    
    [self.iamge2 sd_setImageWithURL:[NSURL URLWithString:listImage1[0]]];
    
    self.iamge3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 5, self.view.frame.size.height / 7 + 5, self.view.frame.size.width / 2 - 10, self.view.frame.size.height / 7 - 5)];
    
    [self.iamge3 sd_setImageWithURL:[NSURL URLWithString:listImage1[1]]];
    
    self.iamge1.userInteractionEnabled = YES;
    self.iamge2.userInteractionEnabled = YES;
    self.iamge3.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
     UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
     UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2:)];
    [self.iamge1 addGestureRecognizer:tap];
    [self.iamge2 addGestureRecognizer:tap1];
    [self.iamge3 addGestureRecognizer:tap2];
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(scroll.bounds.origin.x,scroll.bounds.size.height / 7 * 2 + 5, scroll.bounds.size.width, scroll.bounds.size.height / 7 * 5 - 64 )];

    
    self.table.rowHeight = 64;

    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.scrollEnabled = NO; // 不能滚动
    
    if ([NightModel shareInstance].open == 1) {
        self.table.backgroundColor = [WebColor dimGray];
        self.view.backgroundColor = [WebColor dimGray];
    } else {
        self.table.backgroundColor = [UIColor whiteColor];
    }

    [scroll addSubview:self.iamge3];
    
    [scroll addSubview:self.iamge2];
    
    [scroll addSubview:self.iamge1];
    
 
    
    scroll.contentSize =  CGSizeMake(scroll.bounds.size.width, scroll.bounds.size.height);
    
    
    [self.iamge2 release];
    [self.iamge1 release];

    NSMutableArray *dic1Arr = [NSMutableArray array];
    self.arr1c = [NSMutableArray array];
    for (NSDictionary *dic2 in self.model.list) {
        [dic1Arr addObject:[dic2 objectForKey:@"block_info"]];
        
        
    }
    self.arr1B = [NSMutableArray array];
    for (NSDictionary * dic in dic1Arr) {
        [self.arr1B addObject:[dic objectForKey:@"pic"]];
    }


    [scroll addSubview:self.table];
    [self.view addSubview:scroll];
    [_table release];
    [scroll release];
    
    
}

// 菊花
- (void)theBuffer
{
    self.loading = [[[MBProgressHUD alloc] initWithFrame:self.view.frame]autorelease];
    self.loading.labelText = @"加载....";
    [self.loading setMode:MBProgressHUDModeAnnularDeterminate];
    [self.loading show:YES];
    [self.view addSubview:self.loading];
    
    
}

- (void)tap1:(UITapGestureRecognizer *)tap1
{
    SecondViewController * second = [[SecondViewController alloc] init];
    second.apiString = [[self.model1.list[1][0] objectForKey:@"block_info"] objectForKey:@"api_url"];
    [second setModalTransitionStyle:UINavigationControllerHideShowBarDuration];
    [self presentViewController:second animated:YES completion:^{
        
        
    }];
    
}

- (void)tap2:(UITapGestureRecognizer *)tap2
{
    SecondViewController * second = [[SecondViewController alloc] init];
    second.apiString = [[self.model1.list[1][1] objectForKey:@"block_info"] objectForKey:@"api_url"];
    [second setModalTransitionStyle:UINavigationControllerHideShowBarDuration];
    [self presentViewController:second animated:YES completion:^{
        
        
    }];
    
}
- (void)tap:(UITapGestureRecognizer *)tap
{
    SecondaryPages * second = [[SecondaryPages alloc] init];
    second.aipString = [[self.model1.list[0][0] objectForKey:@"block_topic"] objectForKey:@"api_url"];
    NSLog(@"/*/*12/*31/23*/12*3/*123/%@",second.aipString);

    
    CATransition *animation=[CATransition animation];
    animation.delegate=self;
    animation.duration=0.4;
    animation.type = @"fade";
    animation.subtype = kCATransitionFromLeft;
    [self.navigationController.view.layer addAnimation:animation forKey:@"pageCurl"];
    [self.navigationController pushViewController:second animated:NO];
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    titleLabel.text = @"精选";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    [self startHoms];
    [self startTableView];
    

    
    // Do any additional setup after loading the view.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    myViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
    if (!cell) {
        cell = [[[myViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"1"]autorelease];
    }
        NSMutableArray *dicArr = [[NSMutableArray alloc] init];
       for (NSDictionary *dic in self.model.list) {
            [dicArr addObject:[dic objectForKey:@"block_info"]];
        }
    NSMutableArray *dic1Arr = [NSMutableArray array];

    for (NSDictionary *dic2 in self.model.list) {
        [dic1Arr addObject:[dic2 objectForKey:@"block_info"]];
    }
    NSString *str1 = [dic1Arr[indexPath.row] objectForKey:@"block_color"];
    NSString *str2 = [NSString string];
    str2 = [str1 substringFromIndex:1];
    [cell.imageCell sd_setImageWithURL:[NSURL URLWithString:self.arr1B[indexPath.row]]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        cell.imageCell.image = [cell.imageCell.image imageWithTintColor:[UIColor colorWithHexString:str2]];
        
        cell.imageCell.layer.shadowColor = [UIColor colorWithHexString:str2].CGColor;
        cell.imageCell.layer.shadowOffset = CGSizeMake(4, 4);
        cell.imageCell.layer.shadowOpacity = 1;
//        cell.button.backgroundColor = [UIColor colorWithHexString:str2];
        
       
        
    }];


    cell.label2.text = [dic1Arr[indexPath.row]objectForKey:@"stitle"];
    cell.label2.numberOfLines = 0;
    cell.label2.font = [UIFont systemFontOfSize:14];
    cell.label1.text = [dic1Arr[indexPath.row] objectForKey:@"title"];
    cell.label1.numberOfLines = 0;
    cell.label1.font = [UIFont systemFontOfSize:18];



    


    cell.imageCell.layer.cornerRadius = 30;
    cell.layer.borderColor = UIColor.whiteColor.CGColor;
    
    if ([NightModel shareInstance].open == 1) {
        cell.backgroundColor = [WebColor dimGray];
        self.table.backgroundColor = [WebColor dimGray];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
        self.table.backgroundColor = [UIColor whiteColor];
    }
    

    return cell;
//    [self.table reloadData];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.model.block_info objectForKey:@"title"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SecondViewController * second = [[SecondViewController alloc] init];
    second.apiString = [[self.model.list[indexPath.row] objectForKey:@"block_info"] objectForKey:@"api_url"];
    [second setModalTransitionStyle:UINavigationControllerHideShowBarDuration];
    [self presentViewController:second animated:YES completion:^{
        
      
    }];
    
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    
////    self.iamge.frame = CGRectMake(self.iamge.frame.origin.x, self.iamge.frame.origin.y, self.iamge.frame.size.width + 50, self.iamge.frame.size.height + 50);
//    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"1"];
//    
//    CGRect rect = cell1.imageView.frame;
//    
//    return rect.size.height + 60;
//    [self.table reloadData];
//}

    
    
    



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
